﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CommunityList : System.Web.UI.Page
{
    protected int PageIndex = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["CommunityUser"] == null)
        {
            Response.Redirect("Community.aspx");
        }
        this.DataList2.ItemCreated += new DataListItemEventHandler(DataList2_ItemCreated);
        if (!this.IsPostBack)
        {
            Bind();
            BindView("select top 1 [ID],[Title],[URL],[Price],[UserID] from [GowllCommunitys] where [ID]=38");
        }
        if (!this.IsPostBack)
        {
            Bind();
        }
    }
    private void BindView(string Sql)
    {
        this.DataList2.DataSource = this.SqlDataSource1;
        this.SqlDataSource1.SelectCommand = Sql;
        this.DataList2.DataBind();
    }
    private void Bind()
    {
        SqlConnection MySqlConnection = new SqlConnection();
        MySqlConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["GoWLLConnectionString"].ConnectionString;
        SqlDataAdapter MyDataAdapter = new SqlDataAdapter("Select [ID],[title] from [GowllCommunitys]", MySqlConnection);
        DataSet ds = new DataSet();
        MyDataAdapter.Fill(ds);
        PagedDataSource PageData = new PagedDataSource();
        PageData.DataSource = ds.Tables[0].DefaultView;
        PageData.AllowPaging = true;
        PageData.PageSize = 90;
        if (this.PageIndex == -1)
        {
            this.PageIndex = PageData.PageCount - 1;
        }
        else if (this.PageIndex <= 0)
        {
            this.PageIndex = 0;
            this.LinkButton2.Visible = false;
        }
        else
        {
            this.LinkButton2.Visible = true;
        }
        if (this.PageIndex >= PageData.PageCount - 1)
        {
            this.LinkButton3.Visible = false;
            this.PageIndex = PageData.PageCount - 1;
        }
        else
        {
            this.LinkButton3.Visible = true;
        }
        PageData.CurrentPageIndex = this.PageIndex;
        DataList1.DataSource = PageData;
        DataList1.DataBind();
        this.Label3.Text = this.PageIndex.ToString();

    }
    void DataList2_ItemCreated(object sender, DataListItemEventArgs e)
    {
        this.Label1.Text = ((DataRowView)e.Item.DataItem).Row.ItemArray[1].ToString() + "(" + ((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString() + ")社区";
        string tmp = "价格:" + ((int)((DataRowView)e.Item.DataItem).Row.ItemArray[3]).ToString("0.") + "元 ";
        if (((int)((DataRowView)e.Item.DataItem).Row.ItemArray[4]) == 0)
        {
            tmp += "未售出";
            this.Button1.Visible = true;
            this.Button1.CommandArgument = ((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString();
        }
        else
        {
            tmp += "已售出";
            this.Button1.Visible = false;
        }
        this.Label2.Text = tmp;
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        BindView("select top 1 [ID],[Title],[URL],[Price],[UserID] from [GowllCommunitys] where [ID]=" + ((LinkButton)sender).CommandName);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int UserID = (int)Session["CommunityUserID"];
        int CommunityID = int.Parse(this.Button1.CommandArgument);

        SqlConnection MySqlConnection = new SqlConnection();
        MySqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["GoWLLConnectionString"].ConnectionString;
        MySqlConnection.Open();

        SqlCommand MySqlCommand = new SqlCommand();
        MySqlCommand.CommandType = CommandType.StoredProcedure;
        MySqlCommand.CommandText = "InsertCommunityOrder";
        MySqlCommand.Connection = MySqlConnection;
        MySqlCommand.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, UserID));
        MySqlCommand.Parameters.Add(new SqlParameter("@CommunityID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, CommunityID));
        MySqlCommand.Parameters.Add(new SqlParameter("@OK", SqlDbType.Bit, 1, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
        Application.Lock();
        MySqlCommand.ExecuteNonQuery();
        Application.UnLock();
        if ((bool)MySqlCommand.Parameters[2].Value)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('社区已成功送入购物车。');</script>");
        }
        else
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('社区已经存在与购物车中，不能再次加入。');</script>");
        }
        MySqlConnection.Close();
        MySqlCommand.Dispose();
        MySqlConnection.Dispose();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int len, Price;
        len = this.TextBox1.Text.Trim().Length;
        if (len == 0)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('请输入所申请的社区名称！');</script>");
            return;
        }
        else if (len <= 2)
        {
            Price = 1000;
        }
        else if (len == 3)
        {
            Price = 500;
        }
        else
        {
            Price = 100;
        }
        this.SqlDataSource1.UpdateCommand = "if not exists(select [id] from [GowllCommunitys] where [Title]='" + this.TextBox1.Text + "') begin insert into [GowllCommunitys]([Title],[Price])values('" + this.TextBox1.Text + "'," + Price + ") end";
        if (this.SqlDataSource1.Update() > 0)
        {
            this.BindView("select top 1 [ID],[Title],[URL],[Price],[UserID] from [GowllCommunitys] order by [ID] desc");
            this.PageIndex = -1;
            this.Bind();
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('提交成功，谢谢！');</script>");
        }
        else
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('提交失败，社区已经存在！');</script>");
        }
        this.TextBox1.Text = "";

    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        int.TryParse(this.Label3.Text, out this.PageIndex);
        this.PageIndex--;
        this.Bind();
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        int.TryParse(this.Label3.Text, out this.PageIndex);
        this.PageIndex++;
        this.Bind();
    }
}
